package com.aoxon.cargo.component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Cloth;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.database.DataService;
import com.aoxon.cargo.loader.FileLoader;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.MyService;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.google.gson.reflect.TypeToken;

public class GoodsWaterFall extends WaterFall implements
		NormalDataLoader.OnLoadDataListener {

	private Context context;
	private LayoutInflater inflater;
	private TextView tvBottom;
	private ProgressBar pbBottom;
	private FileLoader myImageLoader = new FileLoader(300, 300, true);
	private NormalDataLoader normalDataLoader;

	private MyService goodsService = MyServiceFactory
			.getInstance(MyServiceFactory.HOT_GOODS);
	private List<Cloth> cloths = new ArrayList<Cloth>();
	private DataService dataService;

	private int width;
	
	protected GoodsWaterFall(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		setup();
	}

	public GoodsWaterFall(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		setup();
	}

	public void setup() {
		inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.pub_list_bottom, null);
		normalDataLoader = new NormalDataLoader((Activity) context);
		normalDataLoader.setOnloadDataListener(this);
		dataService = new DataService(context);
		tvBottom = (TextView) view.findViewById(R.id.tvListBottom);
		tvBottom.setText("向上拉刷新");
		pbBottom = (ProgressBar) view.findViewById(R.id.pbListBottom);
		pbBottom.setVisibility(View.INVISIBLE);
		addFootView(view);
		final Banner banner = new Banner((Activity)context);
		addHeadView(banner.getBanner());
		DisplayMetrics metric = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
		width = metric.widthPixels; // 屏幕宽度（像素）
	}

	
	
	public void load() {
		normalDataLoader.load();
	}

	@Override
	public void scrollToBottom() {
		// TODO Auto-generated method stub
		tvBottom.setVisibility(View.INVISIBLE);
		pbBottom.setVisibility(View.VISIBLE);
	}

	@Override
	public View getView(int position) {
		// TODO Auto-generated method stub
		final Cloth cloth = (Cloth) cloths.get(position);
		View itemView = inflater
				.inflate(R.layout.pur_grid_goods_item_new, null);
		// 图片
		ImageView imageView = (ImageView) itemView
				.findViewById(R.id.ivPurGoodsGridItemNew);
		imageView.setImageResource(R.drawable.loading);
		ProgressBar pBar = (ProgressBar) itemView
				.findViewById(R.id.pbPurGoodsGridItem);
		String path = "";
		if (cloth.getPictures() != null) {
			if (cloth.getPictures().size() <= 1) {
				path = cloth.getPictures().get(0).getPictureUrl() + "m.png";
			} else {
				path = cloth.getPictures().get(0).getPictureUrl() + "m.png";
			}
		}
		myImageLoader.displayImage(path, imageView, pBar, false);
		// 设置图片的高度为图片的宽度
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView
				.getLayoutParams();
		 params.height = width / getColumnCount();
		imageView.setLayoutParams(params);
		//

		TextView tvGoodsName = (TextView) itemView
				.findViewById(R.id.tvGoodsName);
		tvGoodsName.setText(cloth.getClothName());
		TextView tvGoodsPrice = (TextView) itemView
				.findViewById(R.id.tvGoodsPrice);
		if (cloth.getPrices() != null) {
			tvGoodsPrice.setText("￥" + cloth.getPrices().get(0).getPrice());
		}
		TextView tvGoodsCollectNum = (TextView) itemView
				.findViewById(R.id.tvGoodsCollectNum);
		tvGoodsCollectNum.setText(cloth.getCollectedNum() + "");
		return itemView;
	}

	@Override
	public int getContentChildCount() {
		// TODO Auto-generated method stub
		return cloths.size();
	}

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub

	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		try {
			GsonBean gsonBean = goodsService.execute(0, 12);
			if (CheckStateUtil.check(gsonBean)) {
				List<Cloth> newCloths = DataUtil.gson.fromJson(
						gsonBean.getStrJson(), new TypeToken<List<Cloth>>() {
						}.getType());
				if (newCloths.size() > 0) {
					cloths.addAll(newCloths);
				}
			}
		} catch (Exception e) {
			Log.v("cloths", " get database's Clothes");
			// if(0 == 0){
			cloths.addAll(dataService.getClothes(0, 12,
					MyStateUtil.HOME_PAGE_HOT));
			// }else{
			// cloths.addAll(dataService.getClothes(
			// wShowGoods.startIndex, 12,
			// returnType()));
			// }
		}
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		notifyDataSetChanged();
	}

}
